package com.ssyt.hiring.company.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.company.entity.CompanyEntity;
import com.ssyt.hiring.company.service.CompanyService;

public class CompanyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	CompanyService companyService = new CompanyService();

	public CompanyServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("toLoginUI".equals(command)) {
			toLoginUI(request, response);
		} else if ("toRegisterUI".equals(command)) {
			toRegisterUI(request, response);
		} else if ("login".equals(command)) {
			login(request, response);
		} else if ("register".equals(command)) {
			register(request, response);
		} else if ("main".equals(command)) {
			main(request, response);
		} else if ("logout".equals(command)) {
			logout(request, response);
		} else if ("toUpdatePasswordUI".equals(command)) {
			toUpdatePasswordUI(request, response);
		} else if ("updatePassword".equals(command)) {
			updatePassword(request, response);
		} else if ("toQueryCompanyUI".equals(command)) {
			toQueryCompanyUI(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	private void toLoginUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/views/company/company-login.jsp")
				.forward(request, response);
	}

	private void toRegisterUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher(
				"WEB-INF/views/company/company-register.jsp").forward(request,
				response);
	}

	private void toUpdatePasswordUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher(
				"WEB-INF/views/company/update-password.jsp").forward(request,
				response);
	}

	private void login(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String code = request.getParameter("code");
		String password = request.getParameter("password");

		CompanyEntity company = companyService.login(code, password);

		if (company == null) {
			request.setAttribute("code", code);
			request.setAttribute("msg", "用户名或密码错误！");
			toLoginUI(request, response);
		} else {

			request.getSession().setAttribute("company", company);

			request.getRequestDispatcher("CompanyServlet?command=main")
					.forward(request, response);

		}
	}

	private void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String linkman = request.getParameter("linkman");
		String address = request.getParameter("address");
		String numbersStr = request.getParameter("numbers");
		System.out.println(numbersStr);
		int numbers = Integer.parseInt(numbersStr);
		String slogan = request.getParameter("slogan");

		CompanyEntity company = new CompanyEntity();
		company.setCode(code);
		company.setPassword(password);
		company.setName(name);
		company.setEmail(email);
		company.setPhone(phone);
		company.setLinkman(linkman);
		company.setAddress(address);
		company.setNumbers(numbers);
		company.setSlogan(slogan);

		int row = companyService.register(company);
		if (row > 0) {
			request.getRequestDispatcher(
					"WEB-INF/views/company/company-jump.jsp").forward(request,
					response);
		} else {
			request.setAttribute("msg", "注册失败！");
			toRegisterUI(request, response);
		}
	}

	private void main(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1、获取参数
		String rowStr = request.getParameter("row");
		// 非登录操作
		if (rowStr != null) {
			int row = Integer.parseInt(rowStr);
			if (row == 1) {
				request.setAttribute("success", "操作成功！");
			} else {
				request.setAttribute("error", "操作失败！");
			}
		}

		// 转向
		request.getRequestDispatcher(
				"ApplicationServlet?command=listCompanyVerifyByPage").forward(
				request, response);
	}

	private void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().invalidate();

		response.sendRedirect("index.jsp");
	}

	private void updatePassword(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		CompanyEntity companyEntity = (CompanyEntity) request.getSession()
				.getAttribute("company");
		if (companyEntity != null) {
			String code = companyEntity.getCode();
			int id = companyEntity.getId();
			String oldpassword = request.getParameter("oldpassword");
			String password = request.getParameter("password");
			int row = companyService.updatePassword(id, password, oldpassword);

			if (row == 0) {
				request.setAttribute("code", code);
				request.setAttribute("msg", "修改密码失败！");
				toUpdatePasswordUI(request, response);
			} else {
				request.setAttribute("code", code);
				request.setAttribute("msg", "修改密码成功！请重新登录！");
				logout(request, response);
			}
		}
	}

	private void toQueryCompanyUI(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));

		CompanyEntity company = companyService.queryCompany(id);
		if (company != null) {
			request.setAttribute("company", company);
			request.getRequestDispatcher(
					"WEB-INF/views/company/query-company.jsp").forward(request,
					response);
		}

	}

	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String linkman = request.getParameter("linkman");
		String address = request.getParameter("address");
		String numbersStr = request.getParameter("numbers");
		System.out.println(numbersStr);
		int numbers = Integer.parseInt(numbersStr);
		String slogan = request.getParameter("slogan");

		CompanyEntity companyEntity = new CompanyEntity();
		companyEntity.setId(id);
		companyEntity.setName(name);
		companyEntity.setEmail(email);
		companyEntity.setPhone(phone);
		companyEntity.setLinkman(linkman);
		companyEntity.setAddress(address);
		companyEntity.setNumbers(numbers);
		companyEntity.setSlogan(slogan);
		int row = companyService.update(companyEntity);

		if (row == 0) {
			request.setAttribute("id", id);
			request.setAttribute("msg", "修改失败！");
			toQueryCompanyUI(request, response);
		} else {
			request.getSession().setAttribute("company", companyEntity);
			main(request, response);

		}

	}

}
